from django.db import models

# Create your models here.
# 用户表
class UserModel(models.Model):
    name = models.CharField(max_length=100, unique=True, null=True, blank=True)
    mobile = models.CharField(max_length=11, unique=True)
    password = models.CharField(max_length=200, null=True, blank=True)
    email = models.CharField(max_length=20, null=True, blank=True, unique=True)
    pic_url = models.URLField(null=True, blank=True)
    tscore = models.IntegerField(default=0)

# 第三方登录表
class ThirdpartyModel(models.Model):
    userid = models.ForeignKey(UserModel, on_delete=models.CASCADE, null=True, blank=True)
    login_type = models.IntegerField(verbose_name='1->支付宝, 2->微信, 3->qq, 4->微博')
    uid = models.CharField(max_length=100)

# 患者表
class PatientModel(models.Model):
    name = models.CharField(max_length=30, verbose_name='姓名')
    code = models.CharField(max_length=40, verbose_name='身份证')
    sex = models.IntegerField(verbose_name='1&男,2&女')
    is_default = models.BooleanField(verbose_name='是否默认')
    userid = models.ForeignKey(UserModel, on_delete=models.CASCADE)

# 科室表
class Department(models.Model):
    name = models.CharField(max_length=30)
    pid = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='child')
    types = models.IntegerField(verbose_name='1&普通,2三甲')

# 价格表
class FeeModel(models.Model):
    money = models.DecimalField(max_digits=5, decimal_places=2)
    docker_types = models.IntegerField(verbose_name='1&普通,2三甲')
    types = models.IntegerField(verbose_name='1&普通,2&急速')

# 订单表
class MedicalConsultationOrder(models.Model):
    userid = models.ForeignKey(UserModel, on_delete=models.CASCADE, related_name='orders')  # 下单个用户关联多个订单
    doctor_types = models.IntegerField(verbose_name='1&普通,2&三甲')  # 关联医生
    descrip = models.TextField()  # 病情描述，使用TextField存储大文本
    pic_url = models.CharField(max_length=100)
    is_into = models.BooleanField(verbose_name='是否就诊')
    times = models.CharField(max_length=30)  # 持续时间
    patient = models.ForeignKey(PatientModel, on_delete=models.CASCADE)  # 患者
    status = models.IntegerField(verbose_name='支付状态:1&生成,2&已支付,3&支付失败')
    transaction = models.CharField(max_length=50)  # 支付流水号
    pay_type = models.IntegerField(verbose_name='支付方式:1&支付宝,2&微信,3$网银')
    types = models.IntegerField(verbose_name='问诊类型:1&普通,2&极速')
    department = models.ForeignKey(Department, on_delete=models.CASCADE)  # 科室外键
    money = models.IntegerField(default=0)  # 实付金额
    tmoney = models.IntegerField(default=0)  # 应付金额
    score = models.IntegerField(default=0)  # 积分
    couponid = models.IntegerField(default=0)  # 优惠券id
    couponmoney = models.IntegerField(default=0)  # 优惠金额
    orderno = models.CharField(max_length=100, null=True)


# 积分表
class ScoreModel(models.Model):
    userid = models.ForeignKey(UserModel, on_delete=models.CASCADE)
    l_type = models.IntegerField(default=1, verbose_name='积分类型：1=>加积分, 2=>减积分')
    score = models.IntegerField(default=0)
    describe = models.TextField(null=True, blank=True)

# 优惠券表
class CouponsModel(models.Model):
    name = models.CharField(max_length=100)
    types = models.IntegerField(default=1, verbose_name='优惠券的类型:1->注册即送, 2->一月咨询五次')
    money = models.IntegerField(default=0)
    stime = models.DateTimeField(auto_now_add=True)
    etime = models.DateTimeField()
    tcount = models.IntegerField(default=0)
    usecount = models.IntegerField(default=0)  # 使用

# 用户优惠券
class UserCoupons(models.Model):
    userid = models.ForeignKey(UserModel,on_delete=models.CASCADE, verbose_name='用户id')
    couponid = models.ForeignKey(CouponsModel, on_delete=models.CASCADE, verbose_name='优惠券id')
    name = models.CharField(max_length=100, verbose_name='优惠券名称')
    money = models.IntegerField(default=0, verbose_name='抵扣金额')
    stime = models.DateTimeField(auto_now_add=True, verbose_name='发放时间')
    etime = models.DateTimeField(verbose_name='过期时间')
    status = models.BooleanField(default=1)  # 是否可用

# 医生表
class DoctorModel(models.Model):
    name = models.CharField(max_length=30, unique=True)
    Dep_id = models.ForeignKey(Department, on_delete=models.CASCADE, related_name='Dep')
    title = models.CharField(max_length=100, verbose_name='职称')
    pic_url = models.URLField()
    mobile = models.CharField(max_length=11, null=True, blank=True)
    emile = models.CharField(max_length=30, null=True, blank=True)



# 药品分类表
class PharmaceuticalsCate(models.Model):
    name = models.CharField(max_length=100)


# 药品表
class Pharmaceuticals(models.Model):
    name = models.CharField(max_length=100)
    cateid = models.ForeignKey(PharmaceuticalsCate, on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=8, decimal_places=2, default=0)
    pic_url = models.URLField(null=True)


# 处方表
class Prescription(models.Model):
    title = models.CharField(max_length=100)
    orderno = models.CharField(max_length=50)
    doctorid = models.ForeignKey(DoctorModel, on_delete=models.CASCADE)
    userid = models.ForeignKey(UserModel, on_delete=models.CASCADE)
    patienid = models.ForeignKey(PatientModel, on_delete=models.CASCADE)
    status = models.IntegerField(default=1, verbose_name='1、等待支付 2、已支付 3、已完成')
    add_time = models.DateTimeField(auto_now_add=True)
    types = models.IntegerField(default=1, verbose_name='1、支付宝 2、微信 3、网银')
    transaction = models.CharField(max_length=50)
    address = models.CharField(max_length=100)
    phaid = models.ManyToManyField(Pharmaceuticals, related_name='children')


class Address(models.Model):
    id = models.CharField(max_length=100, primary_key=True)
    name = models.CharField(max_length=100, unique=True)
    pid = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        db_table = 'tb_areas'


# 用户地址表
class UserAddress(models.Model):
    userid = models.ForeignKey(UserModel, on_delete=models.CASCADE)
    province = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    district = models.CharField(max_length=100)
    detailaddress = models.CharField(max_length=100)
    contact = models.CharField(max_length=100)
    mobile = models.CharField(max_length=11)
    is_default = models.BooleanField(default=1)



